/*
 * @Author: ShiJunJie
 * @Date: 2020-11-03 10:01:50
 * @LastEditors: ShiJunJie
 * @LastEditTime: 2021-09-07 14:33:20
 * @Descripttion:
 */
export function timeFix() {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9
    ? '早上好'
    : hour <= 11
    ? '上午好'
    : hour <= 13
    ? '中午好'
    : hour < 20
    ? '下午好'
    : '晚上好'
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent() {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

export function handleScrollHeader(callback) {
  let timer = 0

  let beforeScrollTop = window.pageYOffset
  callback = callback || function () {}
  window.addEventListener(
    'scroll',
    (event) => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        let direction = 'up'
        const afterScrollTop = window.pageYOffset
        const delta = afterScrollTop - beforeScrollTop
        if (delta === 0) {
          return false
        }
        direction = delta > 0 ? 'down' : 'up'
        callback(direction)
        beforeScrollTop = afterScrollTop
      }, 50)
    },
    false
  )
}

export function isIE() {
  const bw = window.navigator.userAgent
  const compare = (s) => bw.indexOf(s) >= 0
  const ie11 = (() => 'ActiveXObject' in window)()
  return compare('MSIE') || ie11
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate(id = '', timeout = 1500) {
  if (id === '') {
    return
  }
  setTimeout(() => {
    document.body.removeChild(document.getElementById(id))
  }, timeout)
}

// 防抖
export const _debounce = (fn, delay1) => {
  var delay = delay1 || 200
  var timer
  return function () {
    var th = this
    var args = arguments
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(function () {
      timer = null
      fn.apply(th, args)
    }, delay)
  }
}
// 节流
export const _throttle = (fn, interval1) => {
  var last
  var timer
  var interval = interval1 || 200
  return function () {
    var th = this
    var args = arguments
    var now = +new Date()
    if (last && now - last < interval) {
      clearTimeout(timer)
      timer = setTimeout(function () {
        last = now
        fn.apply(th, args)
      }, interval)
    } else {
      last = now
      fn.apply(th, args)
    }
  }
}

/**
 * 树无限极拍平
 * @param {*} array Array [{'children':[ {''children:[]} ]},{}]
 * @param {*} key 子节点的key
 * @param {*} _response 返回体
 * @returns []
 */
export const patDown = (array = [], key = 'children', _response = []) => {
  array.forEach((e) => {
    _response.push(e)
    e[key] && e[key].length > 0 && patDown(e[key], key, _response)
  })
  return _response

  // return [].concat(...array.map(item => [].concat(item, ...patDown(item[key],key))))
}

/**
 * 树数据过滤并生成新的树
 * patDown2([...array],Object:{ sonK: "children", k: "value", v: "123" })
 * @param {*} array 树
 * @param {*} conditions {sonk=>子节点的Key , k=>判断条件的Key , v=>判断条件的值}
 * @param {*} _response 新的树
 * @returns
 */
export const patDown2 = (
  array = [],
  conditions = { sonK: 'children', k: 'value', v: '123' },
  _response = []
) => {
  _response = array
  for (let k = 0; k < _response.length; k++) {
    const e = _response[k]
    // console.log(e[conditions.sonK].length, e, k, e[conditions.k], conditions.v)
    if (e[conditions.sonK].length === 0 && e[conditions.k] !== conditions.v) {
      _response.splice(k, 1)
      k--
    }
    e[conditions.sonK] &&
      e[conditions.sonK].length > 0 &&
      patDown2(e[conditions.sonK], conditions, _response)
  }
  return _response
}
